package io.wit.common.utils;

import com.alibaba.fastjson.JSONObject;
import io.wit.config.ConfigFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返回数据
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月27日 下午9:59:27
*/
public final class R extends HashMap<String,Object>{

    private static final long serialVersionUID = 1L;
	
	public R(){
		put("code",0);
	}
	
	public static R error() {
		return error("未知异常,请联系管理员");
	}
	
	public static R error(final String msg) {
		return create(500,msg);
	}
	
	public static R create(final int code,final String msg) {
		final R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	/**默认msg=操作成功,code=0*/
    public static R ok(){
        return ok("操作成功");
    }

    public static R execute(final int rows){
        if(rows > 0)
            return ok();
        return fail();
    }

	public static R ok(final String msg) {
		final R r = new R();
		r.put("msg", msg);
		return r;
	}
	
	public static R ok(final Map<String, Object> map) {
		final R r = new R();
		r.putAll(map);
		return r;
	}

    /**默认msg=操作失败,code=199*/
	public static R fail(){
        return fail("操作失败");
	}

	public static R fail(final String msg){
	    return create(199,msg);
	}

	public static R queryEmpty(){
	    return create(201,"暂无数据");
	}

	public static R invalidPrivilege(){
	    return create(401,"没有操作权限");
	}

	public R put(final String key,final Object value) {
		super.put(key,value);
		return this;
	}

    public static R queryJson(final Object object){
        if(object == null || object.toString().trim().length() <= 0){
            return queryEmpty();
        }
        if(object instanceof Map<?,?>){
            final Map<?,?> map = (Map<?,?>) object;
            if(map.size() <= 0){
                queryEmpty();
            }else {
                return new R().put("data",object);
            }
        }
        final JSONObject json = new JSONObject(3);
        if(object instanceof List<?>){
            final List<?> list = (List<?>) object;
            if(list.size() <= 0){
                return queryEmpty();
            }else {
                if (list.get(0) == null){
                    return queryEmpty();
                }else {
                    json.put(ConfigFile.code,ConfigFile.code200);
                    json.put(ConfigFile.msg,ConfigFile.msg200);
                    json.put(ConfigFile.data,object);
                    json.put(ConfigFile.record,((List<?>) object).size());
                    final String jsonObj = json.toJSONString();
                    final JSONObject j = JSONObject.parseObject(jsonObj);
                    final String listData = j.getString(ConfigFile.data);
                    if (listData.equals("[{}]")){
                        return queryEmpty();
                    }
                    return new R().put("data",object);
                }
            }
        }
        if(String.valueOf(object).toLowerCase().equals("null") || String.valueOf(object).replaceAll("\\s*", "").length() == 0){
            return queryEmpty();
        }else {
            return new R().put("data",object);
        }
    }
}